package Controleur;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.*;

import Helpers.Configuration;
import JavaBean.BeanSpectacle;
import Modele.Spectacle;
import Modele.ListeSpectacles;

public class SpectaclesServlet extends CustomServlet
{
	private static final long serialVersionUID = 1L;

	@Override
	public void executeGet(HttpServletRequest request, HttpServletResponse response )
	{
		ListeSpectacles spectacles = (ListeSpectacles)getServletContext().getAttribute("listeSpectacles");
		ArrayList<BeanSpectacle> beanEnvoi = new  ArrayList<BeanSpectacle>();
		String dossierPhotos = (String)getServletContext().getAttribute("dossierPhotos");
		int page = 1;
		int nbPages = (int)Math.ceil(spectacles.size() / Configuration.MAX_SPECTACLES_PAGE);
		
		try
		{
			page = Integer.parseInt(request.getParameter("p"));
		}catch ( Exception e ){}
		
		if ( page > nbPages )
			page = 1;
		
		for ( int i = ( page - 1 ) * Configuration.MAX_SPECTACLES_PAGE + 1; i <= page * Configuration.MAX_SPECTACLES_PAGE && i <= spectacles.size ( ) ; i ++)
		{
			Spectacle e = spectacles.getSpectacle(i);
			BeanSpectacle be = new BeanSpectacle();
			be.setNom(e.getNom());
			be.setLienPhoto(dossierPhotos + e.getId ( ) + ".jpg" );
			be.setId(e.getId());
			beanEnvoi.add(be);
		}
		
		request.setAttribute("spectacles", beanEnvoi);
		request.setAttribute("numPage", page);
		request.setAttribute("nbPages", nbPages );
	}

	@Override
	public void executePost(HttpServletRequest request, HttpServletResponse response)
	{
		// TODO Auto-generated method stub
		
	}
}
